<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="状态" prop="enableFlag">
        <el-select
          v-model="queryParams.enableFlag"
          placeholder="请选择"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in enableFlagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-row>
      <el-col>
        <el-table v-loading="loading" :data="roleList">
          <el-table-column label="编号" prop="id" v-if="columns[0].visible" />
          <el-table-column
            label="版本号"
            prop="version"
            v-if="columns[1].visible"
          />
          <el-table-column
            label="状态"
            prop="enableFlag"
            v-if="columns[2].visible"
          >
            <template slot-scope="scope">
              <span v-if="scope.row.enableFlag == '0'">
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="停用"
                  placement="top-start"
                >
                  <svg-icon
                    icon-class="statetingyong"
                    class-name="card-panel-icon"
                  />
                </el-tooltip>
              </span>
              <span v-else>
                <el-tooltip
                  class="item"
                  effect="dark"
                  content="启用"
                  placement="top-start"
                >
                  <svg-icon
                    icon-class="normalState"
                    class-name="card-panel-icon"
                  />
                </el-tooltip>
              </span>
            </template>
          </el-table-column>

          <el-table-column
            label="版本描述"
            prop="versionDesc"
            v-if="columns[3].visible"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            v-if="columns[4].visible"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="200"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleBannerQuery(scope.row)"
                >详情</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="110px"
        :class="{ showDisabledClas: title == '详情' }"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本号" prop="version">
              <el-input
                v-model="form.version"
                :disabled="title == '详情'"
                placeholder="请输入版本号"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="上传apk" prop="apkPath">
              <miner-file-upload
                v-if="open"
                :value="form.apkPath"
                :current-handle="title"
                @input="getImageUrl"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="状态" prop="enableFlag">
              <el-radio-group
                v-model="form.enableFlag"
                size="small"
                :disabled="title == '详情'"
              >
                <el-radio-button label="1">启用</el-radio-button>
                <el-radio-button label="0">停用</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="强制更新" prop="forceUpdate">
              <el-radio-group
                v-model="form.forceUpdate"
                size="small"
                :disabled="title == '详情'"
              >
                <el-radio-button label="1">强制更新</el-radio-button>
                <el-radio-button label="0">非强制更新</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="版本描述" prop="versionDesc">
              <el-input
                v-model="form.versionDesc"
                type="textarea"
                placeholder="请输入版本描述"
                :disabled="title == '详情'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="变动日志" prop="changeLog">
              <el-input
                v-model="form.changeLog"
                type="textarea"
                placeholder="请输入变动日志"
                :disabled="title == '详情'"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title != '详情'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { versionList, versionDelete, versionAdd, getVersion, versionUpdate } from "@/api/biz"
import { isVersion } from '@/utils/validate'
import MinerFileUpload from '@/components/MinerFileUpload'

export default {
  name: "AppVersion",
  components: {
    MinerFileUpload
  },
  data () {
    const validFormVersion = (rule, value, callback) => {
      if (!isVersion(value)) {
        callback(new Error("版本号格式不对"));
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enableFlag: undefined
      },
      // 表单参数
      form: {
        id: '',
        version: '',  //版本号
        versionDesc: '',  //版本描述
        changeLog: '',  //变动日志
        apkPath: '',  //app安装包位置
        enableFlag: '',  //启用标识
        forceUpdate: ''  //强制更新标记
      },
      enableFlagOptions: [
        {
          dictValue: '1',
          dictLabel: '启用'
        },
        {
          dictValue: '0',
          dictLabel: '停用'
        }
      ],
      // 列信息
      columns: [
        { key: 0, label: `编号`, visible: true },
        { key: 1, label: `版本号`, visible: true },
        { key: 2, label: `状态`, visible: true },
        { key: 3, label: `版本描述`, visible: true },
        { key: 4, label: `创建时间`, visible: true }
      ],
      // 表单校验
      rules: {
        versionDesc: [
          { required: true, message: "请输入版本描述", trigger: "blur" }
        ],
        enableFlag: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        forceUpdate: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        changeLog: [
          { required: true, message: "请输入变动日志", trigger: "blur" }
        ],
        version: [
          { required: true, message: "版本号不能为空", trigger: "blur" },
          { required: true, validator: validFormVersion, trigger: "blur" }
        ],
        apkPath: [
          { required: true, message: "请上传apk", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    getImageUrl (message) {
      this.form.apkPath = ''
      if (message) {
        this.form.apkPath = message;
      }
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加apk版本";
    },
    /** 查看产品信息*/
    handleBannerQuery (row) {
      const bannerId = row.id;
      getVersion(bannerId).then(response => {
        this.form = response;
        this.open = true;
        this.title = "详情";
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const versionId = row.id;
      this.$confirm('是否确认删除version编号为"' + versionId + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return versionDelete(versionId);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 查询列表 */
    getList () {
      this.loading = true;
      versionList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.roleList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: '',
        version: '',  //版本号
        versionDesc: '',  //版本描述
        changeLog: '',  //变动日志
        apkPath: '',  //app安装包位置
        enableFlag: '',  //启用标识
        forceUpdate: ''   //强制更新标记
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      this.form.id = row.id;
      getVersion(this.form.id).then(response => {
        this.form = response;
        this.open = true;
        this.title = "修改";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            versionUpdate(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            versionAdd(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
  }
};
</script>
<style  scoped lang="scss">
.tableImageStyle {
  width: 30px;
  vertical-align: middle;
}
.ant-upload-preview {
  background-color: #fff;
  .avatar-uploader {
    .upload-desc {
      position: absolute;
      bottom: 0;
      left: 0;
      color: #ccc;
      font-size: 8px;
      right: 0;
    }
  }
}
::v-deep .avatar-uploader .el-upload--text {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  margin-right: 20px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep .avatar-uploader-icon {
  font-size: 22px;
  color: #ccc;
  width: 108px;
  height: 108px;
  line-height: 108px;
  text-align: center;
}
::v-deep.avatar {
  width: 108px;
  height: 108px;
  display: block;
}
.showDisabledClas {
  .el-input.is-disabled .el-input__inner,
  .el-textarea.is-disabled .el-textarea__inner {
    color: #606266;
  }
}
</style>
